package com.xml.demo02;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStream;
import java.lang.reflect.Constructor;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class Test01 {
    public static void main(String[] args) throws Exception {
        InputStream ras = Test01.class.getClassLoader().getResourceAsStream("data.properties");
        Properties p=new Properties();
        p.load(ras);
        String className = p.getProperty("className");
        Class<?> aClass1 = Class.forName(className);
        Constructor<?> dc = aClass1.getDeclaredConstructor();
        dc.setAccessible(true);
        Object o = dc.newInstance();
        Method[] declaredMethods = aClass1.getDeclaredMethods();
        BufferedWriter bw=new BufferedWriter(new FileWriter("zuoye.txt",true));
        int index=0;
        for (Method declaredMethod : declaredMethods) {
            try {
                if (declaredMethod.isAnnotationPresent(MyTest.class)) {
                    declaredMethod.setAccessible(true);
                    declaredMethod.invoke(o);

                }
            } catch (Exception e) {
                bw.write(declaredMethod.getName() + "方法出异常了");
                bw.newLine();
                Date d = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss:SS");
                String format = sdf.format(d);
                bw.write("异常出现的时间" + format);
                bw.newLine();
                String simpleName = e.getCause().getClass().getSimpleName();
                bw.write("异常的名字"+simpleName);
                bw.newLine();
                String message = e.getCause().getMessage();
                bw.write("异常的问题"+message);
                bw.newLine();
                bw.flush();
                index++;
            }
        }
        bw.write("本次测试总共产生了"+index+"个异常");
        bw.newLine();
        bw.close();
    }
}
